﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;

namespace LeadExpress.Repository
{
    public partial class UserRepository
    {
        LeadExpressEntities entities = new LeadExpressEntities();

        public User GetSingle(int id)
        {
            return (from c in entities.UserSet where c.ID == id select c).FirstOrDefault();
        }
        public User GetSingle(string username)
        {
            return (from c in entities.UserSet where c.Username == username select c).FirstOrDefault();
        }

        public void Create(User _object)
        {
            try
            {
                string appname = new EFMembershipProvider().ApplicationName;

                _object.CreationDate = System.DateTime.Now;
                _object.IsApproved = true;
                _object.ApplicationName = appname;
                _object.Application = new ApplicationRepository().GetSingle(appname);

                entities.AddToUserSet(_object);
                entities.SaveChanges();
            }
            catch
            {
            }
        }

        public void Delete(int id)
        {
            try
            {
                var _object = (from _list in entities.UserSet where _list.ID == id select _list).First();
                _object.IsApproved = false;
                entities.ApplyPropertyChanges(_object.EntityKey.EntitySetName, _object);
                entities.SaveChanges();
            }
            catch
            {
            }
        }

        public void Save(User _object)
        {
            try
            {
                _object.LastModified = System.DateTime.Now;
                entities.ApplyPropertyChanges(_object.EntityKey.EntitySetName, _object);
                entities.SaveChanges();
            }
            catch
            {
            }
        }

        public void SaveIsLockedOut(User _object)
        {
            try
            {
                _object.LastLockedOutDate = DateTime.Now;
                entities.ApplyPropertyChanges(_object.EntityKey.EntitySetName, _object);
                entities.SaveChanges();
            }
            catch
            {
            }
        }
    }
}